// 206. 反转链表

import { ListNode } from "../1224";

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function reverseList(head: ListNode | null): ListNode | null {
  // 前
  let pre: ListNode | null = head;
  let cur: ListNode | null = null;

  while (pre) {
    // 保存下一下节点
    const next = pre.next;
    // 指针反转
    pre.next = cur;
    // 后前移
    cur = pre;
    //
    pre = next;
  }

  return cur;
}
